#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify, request

from controller.consultingController import ConsultingController
from utils import commons
from utils.response_code import RET, error_map_EN
import json


class ConsultingResource(Resource):

    # get
    @classmethod
    def get(cls, consult_id=None):
        if consult_id:
            kwargs = {
                'consult_id': consult_id
            }

            res = ConsultingController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('auto_id', location='args', required=False, help='auto_id参数类型不正确或缺失')
        parser.add_argument('consult_id', location='args', required=False, help='consult_id参数类型不正确或缺失')
        parser.add_argument('con_id', location='args', required=False, help='con_id参数类型不正确或缺失')
        parser.add_argument('psy_id', location='args', required=False, help='psy_id参数类型不正确或缺失')
        parser.add_argument('start_time', location='args', required=False, help='start_time参数类型不正确或缺失')
        parser.add_argument('end_time', location='args', required=False, help='end_time参数类型不正确或缺失')
        parser.add_argument('is_online', location='args', required=False, help='is_online参数类型不正确或缺失')
        parser.add_argument('address', location='args', required=False, help='address参数类型不正确或缺失')
        parser.add_argument('status', location='args', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('is_delete', location='args', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('create_time', location='args', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='args', required=False, help='update_time参数类型不正确或缺失')

        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = ConsultingController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # delete
    @classmethod
    def delete(cls, consult_id=None):
        if consult_id:
            kwargs = {
                'consult_id': consult_id
            }

        else:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='id不能为空')

        res = ConsultingController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # put
    @classmethod
    def put(cls, consult_id):
        if not consult_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('con_id', location='form', required=False, help='con_id参数类型不正确或缺失')
        parser.add_argument('psy_id', location='form', required=False, help='psy_id参数类型不正确或缺失')
        parser.add_argument('start_time', location='form', required=False, help='start_time参数类型不正确或缺失')
        parser.add_argument('end_time', location='form', required=False, help='end_time参数类型不正确或缺失')
        parser.add_argument('is_online', location='form', required=False, help='is_online参数类型不正确或缺失')
        parser.add_argument('address', location='form', required=False, help='address参数类型不正确或缺失')
        parser.add_argument('status', location='form', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('create_time', location='form', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='form', required=False, help='update_time参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['consult_id'] = consult_id

        res = ConsultingController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def post(cls):
        '''
        处理预约请求
        '''
        data = request.get_json()
        if not data:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='请求数据为空')

        # 检查必填字段
        required_fields = ['psy_id', 'date', 'start_time', 'end_time', 'is_online']
        for field in required_fields:
            if field not in data:
                return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data=f'缺少必填字段: {field}')

        # 设置默认状态为 1（待确认）
        if 'status' not in data:
            data['status'] = 1

        # 调用控制器方法插入数据
        res = ConsultingController.add(**data)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])